﻿/// <reference path="vswd-ext_2.2.js" />

function validValue(value)
{   if(String(value).indexOf('%')>0)
    {
    value=value.substring(0,value.length-1);
    }
    var result=false;
    try{
    if(eval(value)!=undefined)
    {
    result=true;
    }
    }
    catch(Error)
    {
    result=false;
    }
    return result;
}
function getAllCarsSennorReport()
{ 
    var pagesize=20
    var day = new Date();
    var today = day.getFullYear()+'-'+(day.getMonth()+1)+'-'+day.getDate();
    var nowtime = day.getHours()+':'+day.getMinutes();
    var yyday = addDays(day,-1);
    var yestoday = yyday.getFullYear()+'-'+(yyday.getMonth()+1)+'-'+yyday.getDate()
    var fromDateEditor = new Ext.form.DateField({
        renderer:Ext.util.Format.dateRenderer('Y-m-d'),
        format:'Y-m-d',
        value:today 
     });
    
    var intervalTimeEditor = new Ext.form.TimeField({
        value:"00:00",
        format:'H:i',
        width:70,
        increment:5
    });
    
    var fromTimeEditor = new Ext.form.TimeField({
        value:"00:00",
        format:'H:i',
        width:70,
        increment:5
    });
    
    var toDateEditor = new Ext.form.DateField({
        renderer:Ext.util.Format.dateRenderer('Y-m-d'),
        format:'Y-m-d',
        value:today
    });
    
    var toTimeEditor = new Ext.form.TimeField({
        value:nowtime,
        format:'H:i',
        width:70,
        increment:5
    });
    
    var calculatedefinestore = new Ext.data.JsonStore({
        url: 'HandlerGetCalculateDefineInfo',
        root: 'data',
        autoLoad:true,
        fields: [
        'Expression',
        'Calculate',
        'ShowType',
        'Index'
        ]
    });
    var calculateCombo=new Ext.form.ComboBox({
        name:'calculateComboValue',
        store: calculatedefinestore,
        mode:'local',
        displayField:'Expression',
        width:130,
        valueField :'Index',
        hiddenName:'calculateComboValue',
        editable :false,
        triggerAction :'all'
    });
    var downValue=new Ext.form.TextField({width:50,
                    allowBlank: false,
                    blankText:_CustomerConfigResource_CanNotEmpty,
                    validator:validValue,
                    invalidText:_CustomerConfigResource_CValidError});
    var fields = ['StartTime','StartValue','StartAddress','OverTime','OverValue','OverAddress','DeviceID','DeviceName'];
    var historystore = new Ext.data.Store({
		   proxy:new Ext.data.HttpProxy({
                url:"HandlerGetAllCalculateInfoList",
                method:"POST"
           }),
           reader:new Ext.data.JsonReader({
				fields:fields,
                root:"data",
                totalProperty: "totalCount"
           })
	});
    var tbar=new Ext.Toolbar({
        items: [_ReportResources_CalculateExpression+':',
        calculateCombo,
        {
            xtype: 'tbseparator',width:5
        } ,
        _ReportResources_Value+':', 
        downValue,
         _ReportResources_Interval+':', 
        intervalTimeEditor,
        _Alarm_FromTime+':', 
       
        {
            xtype: 'tbseparator',width:5
        } ,
        fromDateEditor ,
        {
            xtype: 'tbseparator',width:5
        },
        fromTimeEditor,
        {
            xtype: 'tbseparator',width:5
        } ,
        _Alarm_ToTime+':', 
        toDateEditor,
        {
            xtype: 'tbseparator',width:5
        } ,
        toTimeEditor,
        //{xtype:'tbfill'},
        {
            tooltip: {text:_TrackerMain_Search, autoHide:true},
            icon:'images/search16.gif',
            handler:function(){
            if (calculateCombo.getRawValue() != ''&&downValue.getValue()!=''&&downValue.validate)
            {
            var store=calculateCombo.getStore();
                    historystore.reload({
                    params:{
                            fromtime:fromDateEditor.getEl().dom.value+' '+ fromTimeEditor.getEl().dom.value,
                            totime:toDateEditor.getEl().dom.value+' '+ toTimeEditor.getEl().dom.value,
                            start:0,
                            limit:pagesize,
                            calculate:store.data.items[calculateCombo.getValue()].data.Calculate,
                            type:store.data.items[calculateCombo.getValue()].data.ShowType,
                            value:downValue.getValue(),
                            interval:intervalTimeEditor.getEl().dom.value
                            }
                });
             }
             else{
             Ext.Msg.alert(_Alarm_NoData,_Alarm_NoData);
             }
                
            }
        }]
    });
    historystore.addListener('load',function(store,records){
    Ext.MessageBox.show({
                            msg: _CustomerConfigResource_Loading,
							progressText: _CustomerConfigResource_Loading,
							width:300,
						    wait:true,
							waitConfig: {interval:2000}
                          });
	    var i;
	    if(records.length>0)
	    {
	    Ext.MessageBox.hide();
	    for(i=0;i<records.length;i++){
	        var valid = records[i].get("gpsavailable");
	        if(valid)
	            records[i].set("gpsexpression" , _TrackerMain_Valid);
	        else
	            records[i].set("gpsexpression" , _TrackerMain_Invalid);    
	    }
	    }
	    else
	    {
	    
         Ext.MessageBox.hide();      
         Ext.Msg.alert(_Alarm_NoData,_Alarm_NoData);
	    }
	});

    var cm = [
            { header: _ReportResources_DeviceID, width: 120, sortable: true, menuDisabled: true, dataIndex: 'DeviceID' },
            { header: _ReportResources_DeviceName, width: 80, sortable: true, menuDisabled: true, dataIndex: 'DeviceName'
                /*,renderer:function(val){
                return Date(val.toString().substring(6,val.toString().length-2));
                }*/
            },
            { header: _ReportResources_StartAddress, width: 150, sortable: true, menuDisabled: true, dataIndex: 'StartAddress'
                /*,renderer:function(val){
                return Date(val.toString().substring(6,val.toString().length-2));
                }*/
            },
            { header: _ReportResources_OverAddress, width: 150, sortable: true, menuDisabled: true, dataIndex: 'OverAddress' },
            { header: _ReportResources_StartTime, width: 120, sortable: true, menuDisabled: true, dataIndex: 'StartTime' },
            { header: _ReportResources_OverTime, width: 120, sortable: true, menuDisabled: true, dataIndex: 'OverTime' },
            { header: _ReportResources_StartValue, width: 60, sortable: true, menuDisabled: true, dataIndex: 'StartValue' },
            { header: _ReportResources_OverValue, width: 60, sortable: true, menuDisabled: true, dataIndex: 'OverValue' }
          
        ];                               
    var grid = new Ext.grid.GridPanel({
        store: historystore,  
        loadMask: true,
        columns: cm,
        width:885,
        height:500
    });




    var calculateUnderTimeWindow = new Ext.Window({
        title: _ReportResources_AllCarsSensorReport,
        height: 590,
        width: 900,
        items: grid,
        tbar: tbar,
        iconCls:'report-calculate',
        closeAction: 'hide',
        minimizable: true,
        constrain:true,
        listeners: {
            'minimize': function() {
                addTaskButtonForWindow(calculateUnderTimeWindow);
            }
        },
        bbar: new Ext.PagingToolbar({
            pageSize: pagesize,
            store: historystore,
            beforePageText: _Alarm_Page,
            refreshTex: _Alarm_Refresh,
            displayMsg: _Alarm_Displaying,
            emptyMsg: _Alarm_Alarm_emptyMsg,
            displayInfo: true
        })
    });
    historystore.on('beforeload',function(store){
    Ext.MessageBox.show({
                            msg: _CustomerConfigResource_Loading,
							progressText: _CustomerConfigResource_Loading,
							width:300,
						    wait:true,
							waitConfig: {interval:2000}
                          });
                          var store1=calculateCombo.getStore();
        historystore.baseParams = {start:'start',limit:'limit',fromtime:fromDateEditor.getEl().dom.value+' '+ fromTimeEditor.getEl().dom.value,totime:toDateEditor.getEl().dom.value+' '+ toTimeEditor.getEl().dom.value,calculate:store1.data.items[calculateCombo.getValue()].data.Calculate,
                            type:store1.data.items[calculateCombo.getValue()].data.ShowType,value:downValue.getValue(),interval:intervalTimeEditor.getEl().dom.value} 
                            
    }) 
     grid.addListener("rowdblclick",function(grid,index){
        var record =grid.getStore().getAt(index);
        var win = createHistoryMapWindow(_TrackerMain_HistoryPoint + '(' + record.get('DeviceName') +')');  
        win.show(); // 显示出来
        nextwindowx += 20;
        nextwindowy += 20;
        var rmap = createMap(win.getMap(),true,null,gmapurl);
        
        rmap.drawSennorEventPoint(record);
        
    });
    calculateUnderTimeWindow.show();
};